#include "ShowFindUpdateRT.h"



bool ShowFindUpdateRT::increaseCompare(Category categ_1, Category categ_2) {
    return categ_1.get_cat_id() < categ_2.get_cat_id();
}

void ShowFindUpdateRT::show(vector<Category> cats, vector<Product> prods) {
    int counterProduct = 0; 		// counter the number of product for each category
	
	cout << "-- List Category --\n";
    
    // Sort Category By ID
    sort(cats.begin(), cats.end(), increaseCompare);
    
    // Show list of categories
    for (int i = 0; i < cats.size(); ++i) {
        cout << cats[i].display() << " :";
		
		// count number product for each category
		for(int j = 0; j< prods.size(); j++) {
			if (cats[i].get_cat_id() == prods[j].get_cat_id()) {
				counterProduct ++;
			}						
		}
		cout << counterProduct << endl;
		counterProduct = 0;		// restart counter number
    }
}

void ShowFindUpdateRT::find(vector<Cart_item> cart_items, vector<Product> prods){
    int tempProductId;
	int numberItem = 0; 	// counter number of item product which we searched
    double sales = 0;		// total sales = numberItem * productPrice
	

    cout << "-- Find Total Sales for a Product --\n";
    
    // Ask user for Product ID
    cout << "Product ID: ";
    cin >> tempProductId;
    
    // Validation Input Product id
    Product prd;			// temporary product found
    bool isExist = false;
    for (int i = 0; i < prods.size(); ++i) {
		
		// input product_id valid
        if (prods[i].get_prod_id() == tempProductId) {
            prd = prods[i];
            isExist = true;
            break;
        }
    }
	
	// input product id not valid
    if (!isExist) {
        cout << "Product id " << tempProductId << " not found." << endl;
        return;
    }

	// check for product_id in cart_items
    for (int i = 0; i < cart_items.size(); ++i) {
        if (cart_items[i].get_prod_id() != tempProductId) continue;

		// product_id found, start counting
        else {
			numberItem = numberItem + cart_items[i].get_prod_qty();
		}
    }
    
	sales = numberItem * prd.get_prod_price();
	
    // Display
    cout << prd.display() << endl;
    cout << "Total sales: " << sales << endl;
}

void ShowFindUpdateRT::update(vector<Product> &prods, vector<Category> cats) {
    int cat_id;
    int prod_id;
    string prod_name;
    double prod_price;
    bool isValidCateg = false; // check for existing category, if category already exist then true
    bool isValidProdID = true; // check for existing productId, if product Id not exist then true

    cout << "-- Add new Product --" << endl;
    cout << "Category ID: ";
    cin >> cat_id;

    // validation category
    for (int i = 0; i < cats.size(); ++i) {
        if (cats[i].get_cat_id() == cat_id) {
            isValidCateg = true; // input category valid
            break;
        }
    }
    if (isValidCateg) {
        cout << "Product ID : ";
        cin >> prod_id;

        // validation prod_id
        for (int j = 0; j < prods.size(); ++j) {
            if (prods[j].get_prod_id() == prod_id) {
                isValidProdID = false; // product id already exist, input product id not valid
                break;
            }
        }

        if (isValidProdID == true) {
            cout << "Product Name: ";
            cin >> prod_name;
            cout << "Product Price: ";
            cin >> prod_price;

            while (prod_price <= 0) {                                           // RA C3
                cout << "Product Price must greater than zero. Try again.\n";   // RA C3
                cout << "Product Price: ";                                      // RA C3
                cin >> prod_price;                                              // RA C3
            }                                                                   // RA C3

            // All input valid, Add to vector
            prods.push_back(Product(prod_id, cat_id, prod_name, prod_price));
        } else {
            cout << "Product ID already exist." << endl;
        }
    } else {
        cout << "Category ID doesn't valid." << endl;
    }
}
